package com.lantosec.hmrp.modules.login.controller;

import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.core.util.RedisUtils;
import com.lantosec.hmrp.modules.login.service.ILoginService;
import com.lantosec.hmrp.modules.system.entity.SysDeptUser;
import com.lantosec.hmrp.modules.system.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Api(value = "用户登录", tags = "用户登录")
@RestController
@RequestMapping("/login")
@Log4j2
@SuppressWarnings("all")
public class LoginController extends BaseController {

    @Autowired
    private ILoginService iLoginService;

    @Autowired
    private RedisUtils redisUtils;


    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@RequestBody SysUser user, HttpServletResponse response, HttpServletRequest request) {
        String userName = user.getUsername();
        String passWord = user.getPassword();
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(passWord)) {
            throw new UnknownAccountException("用户名或密码不能为空");
        }
        Result param = new Result();
        //判断用户名是superuser
        if (userName.equals("superuser")) {
            param = iLoginService.loginSuper(userName, passWord, response, request);
        } else {
            param = iLoginService.login(userName, passWord, response, request);
        }

        return param;
    }

    @PostMapping("/avoidCloseLogin")
    @ApiOperation("免密登录")
    public Result avoidCloseLogin(@RequestBody SysUser user, HttpServletResponse response,HttpServletRequest request) {
        Result param = iLoginService.avoidCloseLogin(user,response,request);
        return param;
    }

    @PostMapping("/choicePositionLogin")
    @ApiOperation("岗位切换返回token数据")
    public Result choicePositionLogin(@RequestBody SysUser user, HttpServletResponse response,HttpServletRequest request) {
        Result param = iLoginService.choicePositionLogin(user,response,request);
        return param;
    }

    @PutMapping("/logout")
    @ApiOperation("用户注销")
    public Result<String> logout(HttpServletRequest request){
        SecurityUtils.getSubject().logout();
        redisUtils.del(getUserId());
        return Result.ok("注销成功");
    }

    @GetMapping("getDeptAndPositionList")
    @ApiOperation(value = "获取用户所有的部门岗位列表")
    public Result<List<SysDeptUser>> getDeptAndPositionList(
            @ApiParam(value = "用户id") @RequestParam(name = "userId") String userId){
        List<SysDeptUser> list = iLoginService.getDeptAndPositionList(userId);
        return Result.ok(list);
    }
}
